﻿@page "/client/report"
@inject HttpClient Http
@inject Blazored.LocalStorage.ISyncLocalStorageService localStorage
@inject NavigationManager NavigationManager
@inject NotificationService ns

<div class="row">
    <div class="col">
        <RadzenTemplateForm TItem="Model" Data="@model" Submit="@OnSubmit">
            <RadzenFieldset Text="上报病例">
                <div class="row" style="margin-bottom: 48px">
                    <div class="col-md-4 align-right">
                        <RadzenLabel Text="患者姓名" />
                    </div>
                    <div class="col">
                        <RadzenTextBox style="display: block" @bind-Value="model.Entity.PatientName" />
                    </div>
                </div>
                <div class="row" style="margin-bottom: 48px">
                    <div class="col-md-4">
                        <RadzenLabel Text="身份证号" />
                    </div>
                    <div class="col">
                        <RadzenTextBox style="display: block" @bind-Value="model.Entity.IdNumber" />
                    </div>
                </div>
                <div class="row" style="margin-bottom: 48px">
                    <div class="col-md-4">
                        <RadzenLabel Text="出生日期" />
                    </div>
                    <div class="col">
                        <RadzenDatePicker @bind-Value="model.Entity.Birthday" />
                    </div>
                </div>
                <div class="row" style="margin-bottom: 48px">
                    <div class="col-md-4">
                        <RadzenLabel Text="当前状态" />
                    </div>
                    <div class="col">
                        <RadzenDropDown TValue="int?"  Placeholder="请选择" Data="@ss" TextProperty="Text" ValueProperty="Value" @bind-Value="model.Entity.Status" />                   
                    </div>
                </div>
                <RadzenButton ButtonType="ButtonType.Submit" Text="提交"></RadzenButton>
            </RadzenFieldset>
        </RadzenTemplateForm>
    </div>
</div>

@code
{
    class Patient
    {
        public string PatientName { get; set; }

        public string IdNumber { get; set; }

        public DateTime? Birthday { get; set; }

        public int? Status { get; set; }

    }

    class Model
    {
        public Patient Entity { get; set; }
    }

    class ComboData
    {
        public string Text { get; set; }
        public int? Value { get; set; }
    }

    Model model = new Model() { Entity = new Patient() };

    List<ComboData> ss = new List<ComboData>(){
        new ComboData(){ Text = "无症状", Value = 0},
        new ComboData(){ Text = "疑似", Value = 1},
        new ComboData(){ Text = "确诊", Value = 2},
        new ComboData(){ Text = "治愈", Value = 3},
        new ComboData(){ Text = "死亡", Value = 4}
    };


    async Task OnSubmit(Model model)
    {
        Http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer" ,localStorage.GetItem<string>("token"));
        try
        {
            StringContent content = new StringContent(JsonSerializer.Serialize<Model>(model), System.Text.Encoding.UTF8, "application/json");
            var res = await Http.PostAsync("/api/Patient/Add", content);
            if (res.IsSuccessStatusCode == true)
            {
                ns.Notify(new NotificationMessage() { Severity = NotificationSeverity.Info, Summary = "提示", Detail = "添加成功", Duration = 4000 });
                NavigationManager.NavigateTo("/client");
           }
            else
            {
                if (res.StatusCode ==  System.Net.HttpStatusCode.Unauthorized)
                {
                    NavigationManager.NavigateTo("/client/");
                }
                if(res.StatusCode == System.Net.HttpStatusCode.BadRequest)
                {
                    var errors = JsonSerializer.Deserialize<WTMError>(await res.Content.ReadAsStringAsync());
                    if (errors.Message.Length > 0)
                    {
                        ns.Notify(new NotificationMessage() { Severity = NotificationSeverity.Error, Summary = "提示", Detail = "添加失败" + errors.Message[0], Duration = 4000 });
                    }
                    foreach (var formerror in errors.Form)
                    {
                        ns.Notify(new NotificationMessage() { Severity = NotificationSeverity.Error, Summary = "提示", Detail = formerror.Value, Duration = 4000 });
                    }
                }
            }
        }
        catch (HttpRequestException ex)
        {
            NavigationManager.NavigateTo("/client/login");

        }

    }
}
